/*
 * 
 * 留存率统计
 * 
 */
$(function(){
	loadDatas();
	//初始化日期控件
  	$('#bt, #et').datetimepicker({
  		language: 'zh-CN',
	    format: 'yyyy-mm-dd',
	    autoclose: true,
	    minuteStep: 1,
	    minView: "month"
	});
	
});

function findAll(){
	
	$("#reset").click();
	
	loadDatas();
}

//查询数据
function loadDatas(){
	//平台
	var platform = $("#platform").val();
	//开始时间
	var begin = $("#begin_time").val();
	//结束时间
	var end = $("#end_time").val();
	
	$.ajax({
		type: "post",
		data: {"platform":platform, "begin":begin, "end":end},
		url: "../../../php/charts/retentionRate/loadRetentionRateCount.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			//表格数据
			var datas1 = msg.lcl1;//平均留存率
			var datas2 = msg.lcl2;//留存率
			var datas3 = msg.lcl3;//平均留存数
			var datas4 = msg.lcl4;//留存数
			
			var htmlStr = "";
			if(datas1.length == 0 && datas2.length == 0 && datas3.length == 0 && datas4.length == 0){
				htmlStr += "<tr><td colspan='14' align='center'>没查询到符合条件的数据~</td></tr>";
			}else{
				
				htmlStr += "<tr style='background-color:#DDECDD;font-weight:bold;'>";
				htmlStr += "<td>"+datas1[0].time+"</td>";
				htmlStr += "<td>"+datas3[0].loginCount+"</td>";
				htmlStr += "<td>"+doubleFormat(datas3[0].lcl1)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas1[0].lcl1)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas3[0].lcl2)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas1[0].lcl2)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas3[0].lcl3)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas1[0].lcl3)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas3[0].lcl7)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas1[0].lcl7)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas3[0].lcl15)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas1[0].lcl15)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas3[0].lcl30)+"</td>";
				htmlStr += "<td>"+doubleFormat(datas1[0].lcl30)+"</td>";
				htmlStr += "</tr>";
				
				for(var i = 0; i<datas2.length; i++){
					htmlStr += "<tr>";
					htmlStr += "<td>"+datas2[i].time+"</td>";
					htmlStr += "<td>"+datas2[i].loginCount+"</td>";
					htmlStr += "<td>"+doubleFormat(datas4[i].lcl1)+"</td>";
					htmlStr += backgroundColorFormat(datas2[i].lcl1);
					htmlStr += "<td>"+doubleFormat(datas4[i].lcl2)+"</td>";
					htmlStr += backgroundColorFormat(datas2[i].lcl2);
					htmlStr += "<td>"+doubleFormat(datas4[i].lcl3)+"</td>";
					htmlStr += backgroundColorFormat(datas2[i].lcl3);
					htmlStr += "<td>"+doubleFormat(datas4[i].lcl7)+"</td>";
					htmlStr += backgroundColorFormat(datas2[i].lcl7);
					htmlStr += "<td>"+doubleFormat(datas4[i].lcl15)+"</td>";
					htmlStr += backgroundColorFormat(datas2[i].lcl15);
					htmlStr += "<td>"+doubleFormat(datas4[i].lcl30)+"</td>";
					htmlStr += backgroundColorFormat(datas2[i].lcl30);
					htmlStr += "</tr>";
				}
			}
			$("#databody").html(htmlStr);
		}
	});
}
//留存率td背景色处理
function backgroundColorFormat(num){
	var str = "<td></td>";
	num = doubleFormat(num);
	if(num >= 0.3){//30%及以上 用红色标识
		str = "<td style='background-color:#ffdfdf;'>"+num+"</td>";
	}else if(num >= 0.2 && num < 0.3){//20%-30% 用紫色标识
		str = "<td style='background-color:#c2addd;'>"+num+"</td>";
	}else if(num >= 0.1 && num < 0.2){//10%-20% 用灰色标识
		str = "<td style='background-color:#ececec;'>"+num+"</td>";
	}else if(num < 0.1 && num > 0){//小于10% 用绿色标识
		str = "<td style='background-color:#aedcae;'>"+num+"</td>";
	}else{
		str = "<td>"+num+"</td>";
	}
	return str;
}
//数字格式处理
function doubleFormat(num){
	if(num == "" || num == 0){
		return "";
	}else{
		var index = num.indexOf(".");
		if(index != -1){
			if(num.substring(0,index) == ""){
				num = "0"+num;
			}
		}
		return num;
	}
}
